﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageUserTable.Queries
{
    /// <summary>
    /// 获取修改员工数据
    /// </summary>
    public class GetUserTableByIdQueries : IRequest<Json<GetUserTableByIdDTO>>
    {
        public int id { get; set; }
    }


    public class GetUserTableByIdQueriesHandler : IRequestHandler<GetUserTableByIdQueries, Json<GetUserTableByIdDTO>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IUsertableRepository _iusertableRepository;
        private readonly IUserdevicetableRepository _iuserdevicetableRepository;
        private readonly IDevicetableRepository _ideviceTableRepository;
        public GetUserTableByIdQueriesHandler(ICurrentUserService icurrentUserServices, IUsertableRepository iusertableRepository, IUserdevicetableRepository iuserdevicetableRepository, IDevicetableRepository ideviceTableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iusertableRepository = iusertableRepository;
            _iuserdevicetableRepository = iuserdevicetableRepository;
            _ideviceTableRepository = ideviceTableRepository;
        }
        public async Task<Json<GetUserTableByIdDTO>> Handle(GetUserTableByIdQueries request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            if (request.id <= 0)
            {
                return Json<GetUserTableByIdDTO>.Failed("请选择有效的员工修改");
            }
            else
            {
                var model = await _iusertableRepository.GetUserTableById(request.id, userid, cancellationToken);
                if (model != null && model.id > 0)
                {
                    List<GetUserTableDeviceList> list = new List<GetUserTableDeviceList>() { };
                    if (model.type == 2)
                    {
                        var list_product = await _ideviceTableRepository.GetDevicetableNameByAdmin(model.id, cancellationToken);
                        if (list_product != null && list_product.Any())
                        {
                            list = list_product.Select(p => new GetUserTableDeviceList()
                            {
                                id = p.id,
                                cusdeviceid = p.cusdeviceid,
                                deviceid = p.deviceid,
                                skuname = p.skuName,
                                userid = model.id
                            }).ToList();
                        }
                    }
                    else
                    {
                        list = await _iuserdevicetableRepository.GetUserTableDeviceList(new List<int>() { model.id }, cancellationToken);
                    }
                    if (!(list != null && list.Any()))
                    {
                        list = new List<GetUserTableDeviceList>() { };
                    }
                    model.list = list;
                    return Json<GetUserTableByIdDTO>.Success("查询成功", model);
                }
                else
                {
                    return Json<GetUserTableByIdDTO>.Failed("暂无权限修改当前员工");
                }
            }

        }
    }
}
